/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:17:32
 * @Last Modified by: songwh
 * @Last Modified time: 2018-04-24 16:41:21
*/

import { modal } from 'vue-strap'
import DatePicker from 'vue-datepicker'

import getDatePickerOption from '../../util/date-picker-option'
import { qiniuUpload, createCastTalk, showMsg, getTypeList } from '../../vuex/action'
export default {
  name: 'cast-talk-item-config',
  data() {
    return {
      showModal: false,
      actionType: 'ADD', // ADD or UPDATE
      voicePicUrl: '',
      form: {
        pubDate: '',
        title: '',
        tags: '',

        newVoiceInfoDtos: [{
          duration: '',
          isQA: '1',
          voiceDetail: '',
          voicePicUrl: '',
          voiceTitle: '',
          voiceUrl: ''
        }],
        voiceTotalSecond: '',
        voiceTypeId: '',

        speaker: '',
        introduction: '',

        meetingPith: '',
        securitiesViewpoint: '',
        guestQA: '',
        buyerQA:'',
        
        buyerQAId: '',
        voiceTotalSize: '',
        performances: [],
        time:{
          hour: '',
          min: '',
          sec: ''
        }
      },
      timePickerOption: getDatePickerOption({
        format: 'YYYY-MM-DD'
      }),
      datePickerOption: getDatePickerOption(),
      classificationList: []
    }
  },
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  vuex: {
    getters: {
      buyerList: state => state.buyerList
    },
    actions: {
      xUpload: qiniuUpload,
      xCreate: createCastTalk,
      xShowMsg: showMsg,
      xGetClassification: getTypeList
    }
  },
  created() {
    this.getClassification({})
  },
  methods: {
    getClassification({start=0, limit=1000}) {
      this.xGetClassification({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.classificationList = res.data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    picFileChange(ctx, index) {
      var tar = ctx.$els.picFile
      if (tar.files.length) {
        var formData = new FormData()
        formData.append('picfile', tar.files[0])
        this.xUpload(formData)
          .then((res) => {
            this.voicePicUrl = res.data.url
          })
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: '上传配图失败。' + res.msg
            })
          })
      }
    },
    voiceFileChange(ctx, index) {
      var tar = ctx.$els.audioFile
      if (tar.files.length) {
        var formData = new FormData()
        formData.append('voicefile', tar.files[0])
        this.xUpload(formData)
          .then((res) => {
            this.form.newVoiceInfoDtos[index].voiceUrl = res.data.url
          })
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: '上传音频失败。' + res.msg
            })
          })
      }
    },
    valid() {
      let { pubDate, title, tags, voiceTotalSize,
            voiceTotalSecond,meetingPith,guestQA,buyerQA,securitiesViewpoint,
            speaker,introduction, time} = this.form
            
      voiceTotalSecond = Number(time.hour*3600) + Number(time.min*60) + Number(time.sec)

      for (let v of [pubDate, title, tags,voiceTotalSize,
        voiceTotalSecond,meetingPith,guestQA,buyerQA,securitiesViewpoint,
        speaker,introduction]) {
        if ((v + '').length === 0) {
          return Promise.reject({
            msg: '* 标记的均为必填项'
          })
        }
      }

      if(!(/(^[1-9]\d*$)/.test(voiceTotalSize))) { 
        return Promise.reject({
          msg: '* 音频总大小必须为整数'
        })
      }

      // for (let it of performances){
      //   let arr = Object.values(it)
      //   for (let i of arr){
      //     if ((i + '').length === 0) {
      //       return Promise.reject({
      //         msg: '* 标记的均为必填项'
      //       })
      //     }
      //   }
      // }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          let {id, pubDate, title, tags,voiceTotalSize, newVoiceInfoDtos,
            performances,voiceTotalSecond,meetingPith,guestQA,buyerQA,securitiesViewpoint,
            speaker, speakerCompany,introduction, time} = this.form

          voiceTotalSecond = Number(time.hour*3600) + Number(time.min*60) + Number(time.sec)
          let params = {
            pubDate: new Date(pubDate).getTime(),
            title: title,
            publishType: -1,
            tags: tags.split(/[,， ]+/),
            voiceTotalSize: voiceTotalSize,
            voiceTotalSecond: voiceTotalSecond,
            meetingPith: meetingPith,
            securitiesViewpoint: securitiesViewpoint,
            guestQA: guestQA,
            buyerQA: buyerQA,
            performances: performances,
            voiceInfoRequestDtos: newVoiceInfoDtos,
            speakerInfos: [{
              speaker: speaker,
              introduction: introduction
            }],
            userAuthType: 1,
            voiceTypeId: this.form.voiceTypeId,
            buyerQAId: this.form.buyerQAId
          }
          if (this.actionType === 'UPDATE') {
            params.id = id
          }

          if(newVoiceInfoDtos && newVoiceInfoDtos.length >0){
            params.voiceInfoRequestDtos[0].voicePicUrl = this.voicePicUrl
          }

          return this.xCreate(params)
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    add(){
      let tmp = {
        duration: '0',
        isQA: '0',
        voiceDetail: '',
        voiceTitle: '',
        voiceUrl: ''
      }
      tmp.id = this.form.newVoiceInfoDtos.size
      this.form.newVoiceInfoDtos.push(tmp)
      
    },
    del(i){
      this.form.newVoiceInfoDtos.splice(i, 1)
    },
    addPerformances(){
      let tmp = {
        type: '1',
        code: '',
        startDate: ''
      }
      tmp.id = this.form.performances.size
      this.form.performances.push(tmp)
    },
    delPerformances(i){
      this.form.performances.splice(i, 1)
    }
  }
}
